package com.hzit.irs.service.impl;

import com.hzit.irs.mapper.MenusMapper;
import com.hzit.irs.model.TbMenus;
import com.hzit.irs.resp.MenuData;
import com.hzit.irs.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private MenusMapper menusMapper;


    @Override
    public List<MenuData> queryMenus() {

        //1.一级菜单列表
         List<MenuData> parentList = new ArrayList<MenuData>();
         List<TbMenus> tbMenus =  menusMapper.queryMenus("0");
         if (CollectionUtils.isEmpty(tbMenus)){
             return null;
         }
         for (TbMenus menus :tbMenus){
             MenuData menuData = new MenuData();
             menuData.setTitle(menus.getTitle());
             menuData.setParentId(menus.getParentId().toString());
             menuData.setHref(menus.getHref());
             menuData.setEnable(true);
             menuData.setIcon("layui-icon-user");
             menuData.setId(menus.getMenuId().toString());

             //获取一级菜单的子菜单
             List<MenuData> childList = new ArrayList<MenuData>();

             List<TbMenus>  childMenuList = menusMapper.queryMenus(menus.getMenuId().toString());
             if (!CollectionUtils.isEmpty(childMenuList)){
                 for (TbMenus childMenu :childMenuList) {
                     MenuData childMenuData = new MenuData();
                     childMenuData.setEnable(true);
                     childMenuData.setIcon("layui-icon-user");
                     childMenuData.setId(childMenu.getMenuId().toString());
                     childMenuData.setTitle(childMenu.getTitle());
                     childMenuData.setParentId(childMenu.getParentId().toString());
                     childMenuData.setHref(childMenu.getHref());
                     childList.add(childMenuData);
                 }
             }
             menuData.setChildren(childList);
             parentList.add(menuData);
         }
        return parentList;
    }
}
